﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<script>
    var currRowId = 0;
    var ifEdit = false;
    var status = 'I';
    $(function () {
        initControl();
        gridList();
        hideEditDiv();
    });
    function initControl() {
        //$('select').bindSelect();
    }
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/MainData/Container/GetPalletListJosn",
            height: $(window).height() - 154,
            colModel: [
                { label: '托盘ID', name: 'PalletID', width: 60, align: 'center', sortable: false, hidden: true },
                { label: '托盘编号', name: 'PalletNO', width: 90, align: 'center', sortable: false },
                { label: '托盘名称', name: 'PalletNOName', width: 120, align: 'center', sortable: false },
                { label: '数量(套)', name: 'PalletSum', width: 80, align: 'right', sortable: false },
                { label: '创建人员', name: 'FCreateBy', width: 80, align: 'left', sortable: false },
                { label: '创建日期', name: 'FCreateDate', width: 80, align: 'center', sortable: false },
                { label: '创建时间', name: 'FCreateTime', width: 80, align: 'center', sortable: false },
                { label: '更改人员', name: 'FUpdateBy', width: 80, align: 'left', sortable: false },
                { label: '更改日期', name: 'FUpdateDate', width: 80, align: 'center', sortable: false },
                { label: '更改时间', name: 'FUpdateTime', width: 80, align: 'center', sortable: false },
                { label: '备注', name: '', width: 20, align: 'center', sortable: false }
            ],
            pager: "#gridPager",
            sortname: 'PalletNO asc',
            viewrecords: true,  //是否显示行数
            rownumWidth: 40,
            gridComplete: function () {

            },
            loadComplete: function () {
                var rowDatas = $gridList.jqGrid("getRowData");
                var length = rowDatas.length + 1;
                if (length > 0) {
                    if (currRowId == 0) {
                        currRowId = 1;
                    } else if (currRowId > length) {
                        currRowId = length;
                    }
                }
                if (currRowId > 0) {
                    $gridList.jqGrid('setSelection', currRowId);
                    var $grid = $('#gview_gridList .ui-jqgrid-bdiv');
                    if (status == 'I') {
                        $grid.setScrollTop(length, currRowId);
                    } else {
                        var top = $grid.scrollTop();
                        $grid.scrollTop(top + 1);
                    }
                }
            },
            onSelectRow: function (rowid) {
                currRowId = rowid;
                $('#btn_remove').removeClass("disabled");
                $('#btn_edit').removeClass("disabled");
                var rowData = $gridList.jqGrid('getRowData', rowid);
                setDataByJson("#eidt_div", rowData);
            },
            beforeSelectRow: function (rowid, e) {
                //if (ifEdit) {
                //    return false;
                //}
            },
            onPaging: function (page_btn) {
                currRowId = 1;
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        $("#btn_search").click(function () {
            reloadGrid();
        });
    }
    function reloadGrid(currNumber) {
        currNumber = currNumber || '';
        var currPage = 1;
        if (currNumber != "") {
            //var data = $("#form1").formSerialize();
            var data = {};
            var page = $('#gridList').getGridParam('page'); // current page  
            var rows = $('#gridList').getGridParam('rowNum'); // rows    
            var sidx = $('#gridList').getGridParam('sortname'); // sidx  
            var sord = $('#gridList').getGridParam('sortorder'); // sord  
            data["sidx"] = sidx;
            data["sord"] = sord;
            data["rows"] = rows;
            data["currNumber"] = currNumber;
            $.ajaxSetup({ async: false });
            $.getJSON("/MainData/Container/GetPalletSortJson", data, function (result) {
                currPage = result.currPage;
                currRowId = result.currIndex;
            });
            $.ajaxSetup({ async: true });
            emptyDataById("#form1");
            isJump = true;
            $("#gridList").jqGrid('setGridParam', {
                page: currPage,
            }).trigger('reloadGrid');
        } else {
            $("#gridList").jqGrid('setGridParam', {
                postData: { Pallet: $("#txt_keyword").val() },
            }).trigger('reloadGrid');
        }
    }
    function hideEditDiv() {
        ifEdit = false;
        $('#eidt_div').hide();
        $('.ui-jqgrid-bdiv').height($('.ui-jqgrid-bdiv').height() + 38);
        //if ($('#btn_add').hasClass("disabled")) {
        //    $('#btn_add').removeClass("disabled");
        //}
        //if ($('#btn_remove').hasClass("disabled")) {
        //    $('#btn_remove').removeClass("disabled");
        //}
        //if ($('#btn_edit').hasClass("disabled")) {
        //    $('#btn_edit').removeClass("disabled");
        //}
        $('#btn_list').show();
    }
    function showEditDiv() {
        ifEdit = true;
        $('#eidt_div').show();
        $('.ui-jqgrid-bdiv').height($('.ui-jqgrid-bdiv').height() - 38);
        //if (!$('#btn_add').hasClass("disabled")) {
        //    $('#btn_add').addClass("disabled");
        //}
        //if (!$('#btn_remove').hasClass("disabled")) {
        //    $('#btn_remove').addClass("disabled");
        //}
        //if (!$('#btn_edit').hasClass("disabled")) {
        //    $('#btn_edit').addClass("disabled");
        //}
        $('#btn_list').hide();
    }
    function btn_add() {
        status = 'I'
        emptyDataById("#eidt_div");
        $('#PalletNO').prop("readonly", false);
        showEditDiv();
    }
    function btn_edit() {
        status = 'U'
        emptyDataById("#eidt_div");
        var rowData = $("#gridList").jqGrid('getRowData', currRowId);
        setDataByJson("#eidt_div", rowData);
        $('#PalletNO').prop("readonly", true);
        showEditDiv();
    }
    function btn_ok() {
        submitForm();
    }
    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        $.submitForm({
            url: "/MainData/Container/SubmitPalletListForm?status=" + status,
            param: $("#form1").formSerialize(),
            success: function (result) {
                hideEditDiv();
                if (status == 'I') {
                    reloadGrid(result.data);
                } else {
                    reloadGrid();
                }
            }
        })
    }
    function btn_cancel() {
        $('.has-error').find('i.error').remove();
        $('.has-error').removeClass("has-error");
        hideEditDiv();
    }
    function btn_remove() {
        var rowData = $("#gridList").jqGrid('getRowData', currRowId);
        $.modalConfirm("注：您确定要【删除】该条数据吗？", function (r) {
            if (r) {
                $.submitForm({
                    url: "/MainData/Container/SubmitPalletListForm?status=D",
                    param: { keyValue: rowData.PalletID },
                    success: function () {
                        reloadGrid();
                    }
                })
            }
        });
    }
    function btn_export() {
        var url = "/Common/ExcelExport?" + encodeURI(init_sql());
        location.href = url;
    }
    function init_sql() {
        var tablename = "ZSJ_22_TP_Pallet";
        var condition = "UseUnit='@FriendStar.Code.Configs.GetValue("UseUnit")'";
        var cols = [];
        //获取列字段
        var colModel = $("#gridList").jqGrid('getGridParam', 'colModel');
        for (var i = 0; i < colModel.length; i++) {
            var columnHidden = colModel[i].hidden;
            var columnName = colModel[i].name;
            var columnLabel = colModel[i].label;
            if (columnHidden == false && columnName != "rn" && columnName != "") {
                if (columnLabel != '') {
                    cols.push(columnName + ' [' + columnLabel.replace(/<[^>]+>/g, "") + ']');
                } else {
                    cols.push(columnName);
                }
            }
        }
        if ($('#txt_keyword').val() != '') {
            condition += " AND (PalletNO LIKE '%" + $('#txt_keyword').val() + "%' OR PalletNOName LIKE '%" + $('#txt_keyword').val() + "%')";
        }
        var sql = "SELECT " + cols.join(',') + " FROM " + tablename + " WHERE " + condition + " ORDER BY PalletNO ASC";
        return "sql=" + sql;
    }
</script>
<div id="layout" style="height: 100%; width: 100%;">
    <form id="form1">
        <div style="padding: 12px 0 5px 0; padding-left: 15px; border-bottom: 1px solid #ccc;" class="line-table input-width-140">
            <div class="btn-group" style="margin-top:-9px;">
                托盘编号/名称<br /><input id="txt_keyword" type="text" class="form-control" /> &nbsp;
            </div>
            <div class="btn-group" style="">
                <a id="btn_search" class="btn btn-primary center-block" onclick=""><span class="glyphicon glyphicon-search"></span> 检索</a>
            </div>
            <div id="btn_list" class="btn-group" style="float:right;">
                <a id="" class="btn btn-primary center-block" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span> 刷新</a>
                <a id="btn_add" class="btn btn-primary center-block" onclick="btn_add()"><span class="glyphicon glyphicon-plus"></span> 新增</a>
                <a id="btn_remove" class="btn btn-primary center-block disabled" onclick="btn_remove()"><span class="glyphicon glyphicon-trash"></span> 删除</a>
                <a id="btn_edit" class="btn btn-primary center-block disabled" onclick="btn_edit()"><span class="glyphicon glyphicon-edit"></span> 修改</a>
                <a id="btn_export" class="btn btn-primary center-block" onclick="btn_export()"><span class="glyphicon glyphicon-download"></span> 导出</a>
            </div>
        </div>
        <div id="eidt_div">
            <table class="form" style="margin:5px 0">
                <tr>
                    <th class="formTitle" style="width:70px;">托盘编号</th>
                    <td class="formValue" style="width:160px;"><input id="PalletNO" name="PalletNO" type="text" class="form-control required" /><input id="PalletID" name="PalletID" type="hidden" /></td>
                    <th class="formTitle" style="width:70px;">托盘名称</th>
                    <td class="formValue" style="width:240px;"><input id="PalletNOName" name="PalletNOName" type="text" class="form-control required" /></td>
                    <th class="formTitle" style="width:70px;">数量(套)</th>
                    <td class="formValue" style="width:60px;"><input id="PalletSum" name="PalletSum" type="text" class="form-control text-right digital" /></td>
                    <td style="padding-left:20px;">
                        <div class="btn-group">
                            <a id="btn_ok" class="btn btn-primary center-block btn-1" onclick="btn_ok()"><span class="glyphicon glyphicon-ok"></span> 输入</a>
                        </div>
                        &nbsp;&nbsp;&nbsp;
                        <div class="btn-group">
                            <a id="btn_cancel" class="btn btn-primary center-block btn-1" onclick="btn_cancel()"><span class="glyphicon glyphicon-remove"></span> 取消</a>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </form>
    <div style="">
        <table id="gridList"></table>
        <div id="gridPager"></div>
    </div>
</div>

